
/**
 * 预览图片
 * @param {string[]|string} urls 
 * @param {any} current 
 * @return 
*/
export function previewImage(urls:string[]|string,current = 0){
	let imgs = []
	if(Array.isArray(urls)) imgs = urls
	else imgs.push(urls)
	uni.previewImage({
		urls:imgs,
		current
	})
}

// 所有的图片后缀
export const imgType = ['png','jpg','jpeg','gif']
// 所有视频后缀
export const videoType = ['mp4','avi','rmvb']
export function getFileIsImg(url:string){
  return imgType.includes(url.split('.').pop() as string)
}
export function getFileIsVideo(url:string){
  return videoType.includes(url.split('.').pop() as string)
}

export function showToast(msg:string,icon?:"success" | "loading" | "error" | "none" | "fail" | "exception", handerl?:Function,time?:number){
	uni.showToast({
		title:msg,
		duration:time || 1500,
		icon:icon || 'success'
	})
	setTimeout(()=> handerl && handerl() , (time || 1500)+200)
}
export function showModal(title:string,content:string,showCancel=true){
	return new Promise((resolve, reject)=>{
		uni.showModal({
			title,
			content,
			showCancel,
			success(e) {
				e.confirm ? resolve(true) : reject()
			},
			fail:()=> reject()
		})
	})
}
/**
 * 提取出对象中指定的key组合为key=value&key=value的格式
 * @param {object} params 
 * @param {string[]} keys 
 * @return 
*/
export function qrString(params:object,keys:string[]){
	var str = '';
	keys.forEach(key=>{
		var value = params[key];
		str += key + '=' + value + '&';
	})
	str = str.slice(0, -1);
	return str;
}

/**
 * 计算两个日期相隔多少小时
 * @param {string} date1 YYYY-MM-DD HH:mm:ss
 * @param {string} date2 YYYY-MM-DD HH:mm:ss
 * @return 
*/
export function calculateHoursDifference(date1:string|Date, date2:string|Date) {
	const d1 = new Date(date1);
	const d2 = new Date(date2);
	const diffTime = Math.abs(d1 - d2);
	const hours = diffTime / (1000 * 60 * 60)
	return hours
}